﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.CUiAPPAutomaticPathPlannerCmdEnabler
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using System;
using System.Collections.ObjectModel;
using Tecnomatix.Engineering;


namespace DnProcessSimulateCommands;

public class CUiAPPAutomaticPathPlannerCmdEnabler : TxCommandSelectionEnabler
{
  protected virtual void OnSelectionCleared() => ((TxCommandEnabler) this)._enable = false;

  protected virtual void OnSelectionItemsAdded(TxObjectList objects)
  {
    ((TxCommandEnabler) this)._enable = this.ShouldEnable();
  }

  protected virtual void OnSelectionItemsRemoved(TxObjectList objects)
  {
    ((TxCommandEnabler) this)._enable = this.ShouldEnable();
  }

  protected virtual void OnSelectionItemsSet(TxObjectList objects)
  {
    ((TxCommandEnabler) this)._enable = this.ShouldEnable();
  }

  private bool ShouldEnable()
  {
    bool flag1 = false;
    CUiAPPAutomaticPathPlannerCmdEnabler.CApAPPTaskTypeProperty taskTypeProperty = CUiAPPAutomaticPathPlannerCmdEnabler.CApAPPTaskTypeProperty.Undefined;
    TxObjectList items = TxApplication.ActiveSelection.GetItems();
    bool flag2 = true;
    try
    {
      if (((Collection<ITxObject>) items).Count > 0)
      {
        if (((Collection<ITxObject>) items).Count == 2 && this.IsValidLocation(((Collection<ITxObject>) items)[0]) && this.IsValidLocation(((Collection<ITxObject>) items)[1]))
        {
          ITxOperation itxOperation1 = ((Collection<ITxObject>) items)[0] as ITxOperation;
          ITxOperation itxOperation2 = ((Collection<ITxObject>) items)[1] as ITxOperation;
          if (itxOperation1 != null && itxOperation2 != null)
          {
            flag1 = ((ITxObject) itxOperation1).Collection == ((ITxObject) itxOperation2).Collection;
            if (flag1)
              flag1 = this.IsValidOperation((ITxObject) ((ITxObject) itxOperation1).Collection, ref taskTypeProperty);
          }
        }
        if (!flag1)
        {
          foreach (ITxObject itxObject in (Collection<ITxObject>) items)
          {
            if (flag2)
              flag2 = this.IsValidOperation(itxObject, ref taskTypeProperty);
            if (!flag2)
              break;
          }
          flag1 = flag2;
        }
        if (!flag1)
        {
          TxObjectList txObjectList = new TxObjectList(items);
          bool flag3 = true;
          ITxTypeFilter itxTypeFilter = (ITxTypeFilter) new TxNoTypeFilter();
          for (int index1 = 0; index1 < ((Collection<ITxObject>) items).Count & flag3; ++index1)
          {
            if (((Collection<ITxObject>) txObjectList).Contains(((Collection<ITxObject>) items)[index1]))
            {
              bool flag4 = this.IsValidOperation(((Collection<ITxObject>) items)[index1], ref taskTypeProperty);
              bool flag5 = this.IsValidLocation(((Collection<ITxObject>) items)[index1]);
              if (flag4)
              {
                ((Collection<ITxObject>) txObjectList).Remove(((Collection<ITxObject>) items)[index1]);
                TxObjectList directDescendants = ((ITxObjectCollection) ((Collection<ITxObject>) items)[index1]).GetDirectDescendants(itxTypeFilter);
                int count = ((Collection<ITxObject>) directDescendants).Count;
                for (int index2 = 0; index2 < ((Collection<ITxObject>) items).Count; ++index2)
                {
                  foreach (ITxObject itxObject in (Collection<ITxObject>) directDescendants)
                  {
                    if (itxObject == ((Collection<ITxObject>) items)[index2])
                    {
                      ((Collection<ITxObject>) txObjectList).Remove(((Collection<ITxObject>) items)[index2]);
                      ((Collection<ITxObject>) directDescendants).Remove(itxObject);
                      break;
                    }
                  }
                }
                flag3 = ((Collection<ITxObject>) directDescendants).Count == 0 || count == ((Collection<ITxObject>) directDescendants).Count;
              }
              else
                flag3 = flag5;
            }
          }
          flag1 = flag3 && ((Collection<ITxObject>) txObjectList).Count == 0;
        }
      }
    }
    catch (Exception ex)
    {
      flag1 = false;
    }
    return flag1;
  }

  private bool IsValidLocation(ITxObject obj)
  {
    switch (obj)
    {
      case TxObjectFlowLocationOperation _:
      case TxWeldLocationOperation _:
      case TxRoboticViaLocationOperation _:
        return true;
      default:
        return obj is TxRoboticSeamOperation;
    }
  }

  private bool IsValidOperation(
    ITxObject obj,
    ref CUiAPPAutomaticPathPlannerCmdEnabler.CApAPPTaskTypeProperty taskTypeProperty)
  {
    bool flag;
    switch (obj)
    {
      case TxObjectFlowOperation _:
        flag = (obj as TxObjectFlowOperation).SimulatedObject != null && taskTypeProperty != CUiAPPAutomaticPathPlannerCmdEnabler.CApAPPTaskTypeProperty.Robotic;
        if (flag)
        {
          taskTypeProperty = CUiAPPAutomaticPathPlannerCmdEnabler.CApAPPTaskTypeProperty.ObjectFlowLike;
          break;
        }
        break;
      case ITxRoboticOrderedCompoundOperation compoundOperation:
        ITxRobot robot = compoundOperation.Robot;
        flag = robot != null && robot.UsesNumericInverse;
        break;
      default:
        flag = false;
        break;
    }
    if (flag && obj is ITxObjectCollection)
      flag = (obj as ITxObjectCollection).Count >= 2;
    return flag;
  }

  private enum CApAPPTaskTypeProperty
  {
    Undefined,
    ObjectFlowLike,
    Robotic,
  }
}
